﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers

// custom headers
#include "CategoriesTreeModel.h"
#include "IGmepGcsCategory.h"
#include "CustomTreeItem.h"
#include "GbmpNew.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    CategoriesTreeModel::CategoriesTreeModel(QObject *parent /*= nullptr*/) 
        : CustomTreeModel(parent)
    {
    }

    CategoriesTreeModel::~CategoriesTreeModel()
    {

    }

    QVariant CategoriesTreeModel::data(const QModelIndex & index, int role) const
    {
        if (!index.isValid())
        {
            return CustomTreeModel::data(index, role);
        }

        CustomTreeItem *item = static_cast<CustomTreeItem*>(index.internalPointer());
        if (item == nullptr)
        {
            return CustomTreeModel::data(index, role);
        }
        switch (role)
        {
        case Qt::DisplayRole:
        case Qt::ToolTipRole:
        {
            auto itemData = item->Data(index.column()).value<void*>();
            if (!itemData)
            {
                break;
            }

            auto category = static_cast<IGmepGcsCategory *>(itemData);
            if (!category)
            {
                break;
            }

            {
                return category->ComponentCount > 0
                    ? QString::fromStdWString(L"%1(%2)").arg(QString::fromStdWString(category->Name)).arg(category->ComponentCount)
                    : QString::fromStdWString(category->Name);
            }

        }
        default:
        {
            break;
        }
        }

        return CustomTreeModel::data(index, role);
    }

    void CategoriesTreeModel::SetCategories(const std::vector<std::shared_ptr<IGmepGcsCategory>>& categories)
    {
        m_vCategories = categories;

        m_mpCategories.clear();
        for (auto &category : categories)
        {
            if (!category)
            {
                continue;
            }

            m_mpCategories[category->Code] = category;
        }

        m_mpTreeItems.clear();
        std::shared_ptr<CustomTreeItem> spRootItem = std::make_shared<CustomTreeItem>(QList<QVariant>() << "category");
        SetupCategoryTree(spRootItem.get(), IGmepGcsCategory());
        SetRoot(spRootItem);
    }

    QModelIndex CategoriesTreeModel::FindIndex(const std::wstring & categoryCode)
    {
        return CustomTreeModel::FindIndex([categoryCode](CustomTreeItem *item) {
            if (!item)
            {
                return false;
            }

            auto itemData = item->Data(0).value<void*>();
            if (!itemData)
            {
                return false;
            }

            auto category = static_cast<IGmepGcsCategory *>(itemData);
            if (!category)
            {
                return false;
            }
            
            return category->Code == categoryCode;
        });
    }

    void CategoriesTreeModel::Init()
    {
    }

    void CategoriesTreeModel::SetupCategoryTree(CustomTreeItem * pParent, IGmepGcsCategory & parentSource)
    {
        if (m_mpCategories.size() == 0)
        {
            return;
        }

        if (!pParent)
        {
            return;
        }

        for (auto &spCategory : m_vCategories)
        {
            if (!spCategory)
            {
                continue;
            }

            bool isChild = false;
            if (pParent->ParentItem() == nullptr)
            {
                if (spCategory->ParentCode.empty())
                {
                    isChild = true;
                }
            }
            else
            {
                if (!spCategory->ParentCode.empty() && spCategory->ParentCode == parentSource.Code)
                {
                    isChild = true;
                }
            }

            if (isChild)
            {
                QList<QVariant> data;
                data << QVariant::fromValue((void*) spCategory.get());
                CustomTreeItem  *child = NEW_AS_QT(CustomTreeItem,data, pParent);

                m_mpTreeItems[spCategory->Code] = child;

                pParent->AppendChild(child);
                SetupCategoryTree(child, *spCategory);
            }
        }
    }

}